import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @author 35326
 * 2021/3/15 19:17
 * jdk1.5 之后，新增了两个创建线程的方法
 * 1：使用Callable接口
 * 2. 使用线程池
 */
public class ThreadCreateTest {
    public static void main(String[] args) {
       NumThread numThread = new NumThread();
        FutureTask<Integer> futureTask = new FutureTask<>(numThread);
        new Thread(futureTask).start();

        Integer integer = null;
        try {
            integer = futureTask.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        System.out.println(integer);


    }
}

class NumThread implements Callable<Integer>{
    @Override
    public Integer call() throws Exception {
        int sum=0;
        for (int i = 0; i < 100; i++) {
            if(i%2==0){
                System.out.println(i);
                sum+=i;
            }
        }
        return sum;
    }
}
